package com.yanqu.road.server.cmd.unionwar;

import com.yanqu.road.logic.cross.CrossUnionWarRankListModel;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.activity.unionwar.UnionWarActivityServerProto;
import com.yanqu.road.pb.cross.activity.CrossActivityProto;
import com.yanqu.road.pb.rank.UnionActivityRankProto;
import com.yanqu.road.server.cmd.CrossUnionCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.logic.MessageHelper;
import com.yanqu.road.server.manager.union.activitygroup.Cross2UnionActivityGroupMgr;
import com.yanqu.road.server.manager.unionwar.CrossUnionWarRankMgr;
import com.yanqu.road.server.protocol.CrossProtocol;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.YanQuMessageUtils;

@Cmd(code = CrossProtocol.C_UNION_WAR_ACTIVITY_GET_RANK_LIST, desc = "获取排行榜列表")
public class CrossUnionWarGetRankListCmd extends CrossUnionCommand {

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        return true;
    }

    @Override
    public void execute(YanQuMessage packet) throws Exception {
        UnionWarActivityServerProto.GetRankListReqMsg reqMsg = UnionWarActivityServerProto.GetRankListReqMsg.parseFrom(packet.getBytes());
        int activityId = reqMsg.getActivityId();
        int stage = reqMsg.getStage();
        CrossActivityProto.UnionWarRankListRespMsg.Builder respMsg = CrossActivityProto.UnionWarRankListRespMsg.newBuilder();
        respMsg.setActivityId(activityId);
        respMsg.setMyRank(-1);
        respMsg.setValue("0");
        respMsg.setStage(stage);
        CrossUnionWarRankListModel crossUnionWarRankListModel = CrossUnionWarRankMgr.getCrossUnionWarRankListModel(activityId, stage, groupId);
        if (crossUnionWarRankListModel != null) {
            respMsg.setRankList(crossUnionWarRankListModel.getRankListMsg());
            String unionUid = Cross2UnionActivityGroupMgr.getUnionUid(activityId, packet.getPlayerId());
            int myRank = crossUnionWarRankListModel.getMyRank(serverId, unionUid);
            respMsg.setMyRank(myRank);
            for (UnionActivityRankProto.UnionActivityRankTempMsg tempMsg : respMsg.getRankList().getRankListList()) {
                if(tempMsg.getUnionUid().equals(unionUid)){
                    respMsg.setValue(tempMsg.getValue());
                }
            }
        }else {
            respMsg.setRankList(UnionActivityRankProto.UnionActivityRankListMsg.newBuilder());
        }
        MessageHelper.sendPacket(serverId, packet.getPlayerId(), YanQuMessageUtils.buildMessage(Protocol.U_UNION_WAR_ACTIVITY_GET_RANK_LIST, respMsg));
    }

    @Override
    public void sendData() {

    }
}
